﻿using System.Collections.Generic;
using System.Data;
using Navigant.Infrastructure;
using Navigant.Infrastructure.Cases;
using Navigant.Infrastructure.RepositoryFramework;
using Navigant.Infrastructure.DomainBase;

namespace Navigant.Model.PreservationNotices
{
    public class PreservationNoticeService : CaseService<PreservationNotice>
    {
        public PreservationNoticeService(ICase currentCase)
            : this(currentCase, null)
        {
        }

        public PreservationNoticeService(ICase currentCase, IUnitOfWork unitOfWork)
            : this(currentCase, unitOfWork, false)
        {
        }

        public PreservationNoticeService(ICase currentCase, IUnitOfWork unitOfWork,
            bool runWithElevatedPrivileges)
            : base(currentCase, unitOfWork, runWithElevatedPrivileges)
        {
        }

        protected override IRepository<PreservationNotice> GetRepository()
        {
            return RepositoryFactory.GetRepository<IPreservationNoticeRepository, PreservationNotice>(this.CurrentCase, 
                this.UnitOfWork, this.RunWithElevatedPrivileges);
        }

        public PreservationNotice GetPreservationNotice(int preservationNoticeId)
        {
            return this.Repository.FindBy(preservationNoticeId);
        }

        public IList<PreservationNotice> GetPreservationNoticesBy(int preservationNoticeTemplateId)
        {
            return ((IPreservationNoticeRepository)this.Repository).FindBy(preservationNoticeTemplateId);
        }

        public IList<PreservationNotice> GetPreservationNoticesBySite()
        {
            return ((IPreservationNoticeRepository)this.Repository).FindBySite();
        }

        public DataTable GetPreservationNoticesBySiteRaw()
        {
            return ((IPreservationNoticeRepository)this.Repository).FindBySiteRaw();
        }

        public IList<PreservationNotice> GetPreservationNoticesBySite(int pageSize, int currentPage, out int itemCount)
        {
            return ((IPreservationNoticeRepository)this.Repository).FindBySite(pageSize, currentPage, out itemCount);
        }

        public IList<PreservationNotice> FindBySiteAnd(string messageIdentifier)
        {
            return ((IPreservationNoticeRepository)this.Repository).FindBySiteAnd(messageIdentifier);
        }

        public IList<PreservationNotice> FindBySiteAnd(int custodianId)
        {
            return ((IPreservationNoticeRepository)this.Repository).FindBySiteAnd(custodianId);
        }

        public IList<PreservationNotice> FindBySiteAnd(int custodianId, 
            int preservationNoticeTemplateId)
        {
            return ((IPreservationNoticeRepository)this.Repository).FindBySiteAnd(preservationNoticeTemplateId, 
                custodianId);
        }

        public IList<PreservationNotice> FindByStatus(PreservationNoticeStatus status, int maxResults)
        {
            return ((IPreservationNoticeRepository)this.Repository).FindByStatus(status, maxResults);
        }

        public IList<PreservationNotice> FindAllUnsent(int maxResults)
        {
            return ((IPreservationNoticeRepository)this.Repository).FindAllUnsent(maxResults);
        }

        public IList<Attachment> FindAttachments(int preservationNoticeId)
        {
            return ((IPreservationNoticeRepository)this.Repository).FindAttachments(preservationNoticeId);
        }

        public void SavePreservationNotice(PreservationNotice notice)
        {
            this.Repository[notice.Key] = notice;
        }

        public void AddPreservationNotice(PreservationNotice notice)
        {
            this.Repository.Add(notice);
        }

        public void RemovePreservationNotice(PreservationNotice notice)
        {
            this.Repository.Remove(notice);
        }

        public void AssociateToLogEntry(PreservationNotice notice, SmtpLogEntry logEntry)
        {
            ((IPreservationNoticeRepository)this.Repository).AssociateToLogEntry(notice, logEntry);
        }

        public SmtpLogEntry FindSmtpLogEntry(PreservationNotice notice)
        {
            return ((IPreservationNoticeRepository)this.Repository).FindSmtpLogEntry(notice);
        }

        public SmtpLogEntry FindSmtpLogEntry(PreservationNotice notice, bool findAllSmtpCodes)
        {
            return ((IPreservationNoticeRepository)this.Repository).FindSmtpLogEntry(notice, findAllSmtpCodes);
        }

        public void UpdateVerificationTries(PreservationNotice notice)
        {
            ((IPreservationNoticeRepository)this.Repository).UpdateVerificationTries(notice);
        }
    }
}
